import { article as ArticleApi } from '../../../../../config/request.js';
import resources from '../../../../../util/resources.js';
import { Loading } from 'element-ui';


module.exports = {
    name: 'article',
    data() {
        return {
            select: '',
            checked: [],
            tableData: [],
            status: {
                info: { prop: 'status', label: '文章状态', sortable: true },
                filter: {
                    list: [{
                        text: '启用',
                        value: 1
                    }, {
                        text: '禁止',
                        value: 0
                    }],
                    multiple: false
                },
                style: { align: 'center' }
            },
            type: {
                info: { prop: 'type', label: '文章类型', sortable: true },
                filter: {
                    list: [{
                        text: '普通文章',
                        value: 1
                    }, {
                        text: '精华文章',
                        value: 2
                    }, {
                        text: '热点文章',
                        value: 3
                    }, {
                        text: '精华热点文章',
                        value: 4
                    }],
                    multiple: false
                },
                style: { align: 'center' }
            },
            title: {
                info: { prop: 'title', label: '文章标题', sortable: false },
                filter: {},
                style: { align: 'center' }
            },
            remark: {
                info: { prop: 'remark', label: '文章备注', sortable: false },
                filter: {},
                style: { align: 'center' }
            },
        }
    },

    methods: {
        /**
         * 格式化状态
         */
        formatterStatu(item) {
            return item.status == 1 ? '启动' : '禁止';
        },
        /**
         * 过滤状态
         */
        filterStatu(value, item) {
            return item.status == value;
        },

        /**
         * 过滤article状态
         */
        filterType(value, item) {
            return item.type == value;
        },
        /**
         * 格式化article状态
         * 
         * @param {any} item 
         * @returns 
         */
        formatterType(item) {
            for (let i in resources.aType) {
                if (item.type == resources.aType[i].value) {
                    return resources.aType[i].label
                }
            }
        },
        /** 删除 */
        onDeleteArticle(article, index) {
            this.$confirm('是否确认删除该文章？', '提示', {
                cancelButtonText: '取消',
                confirmButtonText: '确定',
                type: 'warning'
            }).then(_ => {
                this.tableData.splice(index, 1);
                ArticleApi.deleteArticle.call(this, {
                    id: article.id,
                }, data => {
                    this.$message.success('删除成功');
                })
            }).catch(_ => {});
        },
        /** 置顶 */
        onMoveTop(article, index) {
            this.$confirm('是否确认置顶该文章？', '提示', {
                cancelButtonText: '取消',
                confirmButtonText: '确定',
                type: 'warning'
            }).then(_ => {
                this.tableData.splice(index, 1);
                ArticleApi.moveTop.call(this, {
                    id: article.id,
                }, data => {
                    this.$message.success('置顶成功');
                    const newArticle = data['article'];
                    Object.assign(newArticle, { checked: newArticle.status == 1 });
                    this.tableData.splice(0, 0, newArticle);
                })
            }).catch(_ => {});
        },
        /**
         * 启用或禁用该article
         * 
         * @param {any} article 
         */
        onCheckedChange(article) {
            ArticleApi.updateArticleStatus.call(this, {
                id: article.id,
                status: article.checked ? 1 : 0
            }, data => {
                article.status = data.article.status;
                this.$message.success(data.article.status == 1 ? '已经启用' : '已经禁用');
            })

        },
        initData() {
            //查询所有数据
            this.getArticleData();
        },
        checkLevel() {
            var user = this.$store.state.user.userinfo;
            return user && user.level == 1;
        },
        addArticle() {
            this.$router.push('/business/htmlconfig/editor?updateFlag=' + false);
        },
        updatedArticle(article) {
            this.$router.push('/business/htmlconfig/editor?updateFlag=' + true + '&aid=' + article.id);
        },
        getArticleData() {
            var that = this;
            ArticleApi.queryAll.call(this, (data) => {
                console.log(data)
                data.articles.forEach(article => {
                    Object.assign(article, { checked: article.status == 1 });
                });
                this.tableData = data.articles;
            });
        },
    },

    mounted: function() {
        this.initData();
    }
}